﻿
Partial Class WebForm_SearchRoleProgramColumnMappingByRole
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.Visible = True
    End Sub

    Protected Sub chkAll_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim chkAll As CheckBox = TryCast(sender, CheckBox)
        Dim cellIndex = Me.gv.HeaderRow.Cells.GetCellIndex(chkAll.Parent)

        For Each row As GridViewRow In Me.gv.Rows
            Dim chkAuth As CheckBox = TryCast(row.Cells(cellIndex).Controls(1), CheckBox)

            chkAuth.Checked = chkAll.Checked
        Next

        Me.Show()
    End Sub

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Me.AddRoleAuthMapping()
        Me.Show(Me.RoleID, Me.ProgramID)
    End Sub
End Class

Partial Class WebForm_SearchRoleProgramColumnMappingByRole
    Private authList() As String = {"Query", "Add", "Edit", "Delete", "Print", "Admin"}

    ''' <summary>
    ''' 僅顯示，不重載資料
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub Show()
        Me.mpSearch.Show()
        Me.Visible = True

        RaiseEvent Shown(Me, New EventArgs)
    End Sub

    ''' <summary>
    ''' 顯示且重載資料
    ''' </summary>
    ''' <param name="roleID"></param>
    ''' <remarks></remarks>
    Public Sub Show(ByVal roleID As String, ByVal programID As String)
        Me.RoleID = roleID
        Me.programID = programID
        Me.BindData()
        Me.BindGV()

        Me.mpSearch.Show()
        Me.Visible = True

        RaiseEvent Shown(Me, New EventArgs)
    End Sub

    Private Sub BindData()
        If clsAuth.GetTableAuth("RoleProgramMapping", "Edit") = False Then
            Me.btnSave.Enabled = False
        Else
            Me.btnSave.Enabled = True
        End If
    End Sub

    Private Sub BindGV()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.Length = 0
        sql.AppendFormat("select p.ProgramID, p.ProgramName, c.name as ColumnName, ep.value as ColumnNameText ")
        sql.AppendFormat(", 'Query' as QueryAccess ")
        sql.AppendFormat(", 'Add' as AddAccess ")
        sql.AppendFormat(", 'Edit' as EditAccess ")
        sql.AppendFormat(", 'Delete' as DeleteAccess ")
        sql.AppendFormat(", 'Print' as PrintAccess ")
        sql.AppendFormat(", 'Admin' AdminAccess ")
        sql.AppendFormat("from syscolumns c ")
        sql.AppendFormat("inner join sysobjects o on c.id = o.id ")
        sql.AppendFormat("inner join Program p on o.name = p.TableName ")
        sql.AppendFormat("left join sys.extended_properties ep on c.id = ep.major_id and c.colid = ep.minor_id and ep.name = 'MS_Description' ")
        sql.AppendFormat("where p.ProgramID = '{0}' ", Me.ProgramID)

        'sql.AppendFormat("order by ProgramName ")

        Me.gv.DataSource = dat.GetData(sql.ToString(), Nothing, Data.CommandType.Text, "dt")
        Me.gv.DataBind()

        sql.Length = 0
        sql.AppendFormat("select ro.*, r.RoleName from RoleProgramMapping ro ")
        sql.AppendFormat("inner join Role r on ro.RoleID = r.RoleID ")
        sql.AppendFormat("where r.RoleID = '{0}' ", Me.RoleID)
        sql.AppendFormat("and ro.ProgramID = '{0}' ", Me.ProgramID)
        sql.AppendFormat("and ro.ColumnName <> '' ")

        Dim mappings As Data.DataTable = dat.GetData(sql.ToString, Nothing, Data.CommandType.Text, "dt").Tables(0)

        For Each row As GridViewRow In Me.gv.Rows
            Dim key As DataKey = Me.gv.DataKeys(row.RowIndex)

            For Each auth As String In Me.authList

                Dim chkAuth As CheckBox = TryCast(row.FindControl("chk" + auth), CheckBox)
                Dim cellIndex = row.Cells.GetCellIndex(chkAuth.Parent)
                Dim chkAll As CheckBox = TryCast(Me.gv.HeaderRow.Cells(cellIndex).Controls(1), CheckBox)

                mappings.DefaultView.RowFilter = String.Format("ColumnName = '{0}' and AuthName = '{1}' ", key("ColumnName"), auth)

                If mappings.DefaultView.Count > 0 Then
                    chkAuth.Checked = True
                Else
                    chkAuth.Checked = False
                End If

                chkAll.Checked = chkAll.Checked And chkAuth.Checked
            Next
        Next
    End Sub

    Private Function AddRoleAuthMapping() As Boolean
        Dim sql As New System.Text.StringBuilder
        Dim dat As New clsData

        Using scope As New Transactions.TransactionScope

            sql.Length = 0
            sql.AppendFormat("delete RoleProgramMapping ")
            sql.AppendFormat("where RoleID = '{0}' ", Me.RoleID)
            sql.AppendFormat(" and ProgramID = '{0}' ", Me.ProgramID)

            dat.AccData(sql.ToString, Nothing, Data.CommandType.Text)

            For Each row As GridViewRow In Me.gv.Rows
                Dim key As DataKey = Me.gv.DataKeys(row.RowIndex)

                For Each auth As String In Me.authList
                    Dim chkAuth As CheckBox = TryCast(row.FindControl("chk" + auth), CheckBox)

                    If chkAuth.Checked = True Then
                        sql.Length = 0
                        sql.AppendFormat("insert RoleProgramMapping (RoleID,ProgramID,AuthName,ColumnName) select ")
                        sql.AppendFormat(" '{0}' ", Me.RoleID)
                        sql.AppendFormat(",'{0}' ", Me.ProgramID)
                        sql.AppendFormat(",'{0}' ", auth)
                        sql.AppendFormat(",'{0}' ", key("ColumnName"))

                        dat.AccData(sql.ToString, Nothing, Data.CommandType.Text)
                    End If
                Next

            Next

            scope.Complete()
        End Using

        Return True
    End Function

    Public Property RoleID() As String
        Get
            Return ViewState("RoleID")
        End Get
        Set(ByVal value As String)
            ViewState("RoleID") = value
        End Set
    End Property

    Public Property ProgramID() As String
        Get
            Return ViewState("ProgramID")
        End Get
        Set(ByVal value As String)
            ViewState("ProgramID") = value
        End Set
    End Property

    Public Event Shown(ByVal sender As Object, ByVal e As System.EventArgs)
End Class
